package com.jrx.anytxn.param.job.autoPaymentStatus;

import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.mapper.ext.ExtBtBatchStatusMapper;
import com.jrx.anytxn.param.mapper.ext.ExtPrOrganizationTableMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * 批扣状态更新
 *
 * @author : zhenghaopu
 * 2019/11/23
 */
@Configuration
public class AutoPaymentBatchStatusUpdateConfiguration {
    private static final Logger logger = LoggerFactory.getLogger(AutoPaymentBatchStatusUpdateConfiguration.class);


    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Resource
    private ExtPrOrganizationTableMapper extPrOrganizationTableMapper;


    @Resource
    private ExtBtBatchStatusMapper extBtBatchStatusMapper;

    @Bean
    public Job autoPaymentBatchStatusUpdateJob() {
        return jobBuilderFactory.get("autoPaymentBatchStatusUpdateJob")
                .start(autoPaymentBatchStatusUpdateStep())
                .build();
    }

    @Bean
    public Step autoPaymentBatchStatusUpdateStep() {
        return stepBuilderFactory.get("autoPaymentBatchStatusUpdateStep")
                .tasklet(autoPaymentBatchStatusUpdateTasklet())
                .build();
    }

    @Bean
    public Tasklet autoPaymentBatchStatusUpdateTasklet() {
        return (stepContribution, chunkContext) -> {
            logger.info("批扣结束");
            //获取业务日期
            PrOrganizationTable org =
                    extPrOrganizationTableMapper.selectByOrgIdForBatch(Constant.DEFAULT_ORG_NUMBER);
            //更新执行中的job为执行中
            extBtBatchStatusMapper.updateByBusinessDateAndStatusForBatch(org.getNextProcessingDate(),
                    Constant.BATCH_SATTUS_0,Constant.BATCH_SATTUS_1, Constant.AUTO_PAYMENT);
            return RepeatStatus.FINISHED;
        };
    }
}
